import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router';

const routes: RouteRecordRaw[] = [
  {
    path: '/',
    name: 'Index',
    redirect: '/aiTest',
    component: () => import('@/views/index.vue'),
    meta: {
      title: 'AI-新闻',
    },
    children: [
      {
        path: '/aiTest',
        name: 'AiTest',
        component: () => import('@/views/AI-test.vue'),
        meta: {
          title: 'AI-测试',
        },
      },
    ],
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/login.vue'),
    meta: {
      title: '登录',
    },
  },
];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});
router.beforeEach((to, from, next) => {
  const name = localStorage.getItem('user_token');
  if (!name && to.path !== '/login') {
    next('/login');
  } else {
    next();
  }
});
export default router;
